<?
//include("funk/admin/$db->backup.funk.php");
//Überprüfen ob der Abruf über die Hauptseite erfolgt
if(!defined('InStern')) exit;
include("conf/fraktionen.conf.php");
//Globale werte
if(isset($_GET[fraktion])) $_SESSION[charpics][fraktion]=intval($_GET[fraktion]);
if($_SESSION[charpics][fraktion]==0) $_SESSION[charpics][fraktion]=1;
if(isset($_GET[select]) AND isset($_GET[set]))
  $_SESSION[charpics][$_GET[select]]=$_GET[set];
//Funktionen
function uploadform($editor, $typ, $need=0)
{
 return "
  <form action=\"index.php\" method=\"post\" enctype=\"multipart/form-data\" id=\"upload$editor$typ\">
    <input type=\"hidden\" name=\"editor\" value=\"$editor\" />
    <input type=\"hidden\" name=\"typ\" value=\"$typ\" />
    <input type=\"hidden\" name=\"need\" value=\"$need\"
    <input type=\"file\" name=\"bild\" size=\"1\" onchange=\"document.getElementById('upload$editor$typ').submit();\">
  </form>";
}
function imageselect($editor,$typ,$need=0)
{
  global $db;
  $match=false;
  $return="
  <select style=\"width:130px;\" onchange=\"location.href='index.php?select=".$editor."-".$typ."&set='+this.value;\" id=\"selectbox$typ\">
    <option value=\"0\">-wählen-</option>";  
  if($need!=0) $n=" AND Need=$need ";
  $result=$db->query("SELECT * FROM charpics WHERE Fraktion=".intval($_SESSION[charpics][fraktion])." AND Editor=$editor AND Typ=$typ $n;");
  while($line=$db->fetch_assoc($result))
  {
    if($line[Id]==$_SESSION[charpics][$editor."-".$typ]) 
    {
      $return.="<option value=\"$line[Id]\" selected=\"selected\">$line[Name]</option>";
      $match=true;
    }
    else
      $return.="<option value=\"$line[Id]\">$line[Name]</option>";
    }
  $return.= "
  </select>";
  if(!$match) $_SESSION[charpics][$editor."-".$typ]=0;
  return $return;
}
//ENDE DER FUNKTIONEN
//Konsistenz des Systems prüfen
if($_GET['erase']>0){
  $db->query("DELETE FROM charpics WHERE Id=".intval($_GET['erase']));
}
if(!isset($_SESSION[constistenzscheck]))
{
    $abfrage="SELECT * FROM charpics;";
	$result=$db->query($abfrage);
	while($pic=$db->fetch_assoc($result))
	{
	    if(!file_exists("images/kommander/kommander".$pic[Id].".png"))
        {		
		    $fehlt[]=$config_fraktion[$pic[Fraktion]][name]."/".$pic[Name];
		}
	}
    if(isset($fehlt))
    {
        echo "<div class=\"error\">Folgende Kommanderbilder konnten nicht im Dateisystem gefunden werden: ".implode(", ",$fehlt)."<br><br>Diese Fehler sollte behoben werden, bevor neue Bilder hinzugefügt werden!</div>";		
    }
	else
	{
	  $_SESSION[constistenzscheck]=true;
	}
}
if(isset($_FILES[bild]))
{ //hochgeladenes Bild
  //Dateinamen auswerten
  $datei=explode(".",$_FILES[bild][name]);
  if($_SESSION[charpics][fraktion]==0)
    echo "<div class=\"error\">Es wurde keine Fraktion ausgewählt</div>";
  elseif($datei[1]!="png")
    echo "<div class=\"error\">Das Bild muss unbedingt ein transparentfähiges PNG sein.</div>";
  else
  {
    //Id ermitteln
	$db->query("SELECT Id FROM charpics WHERE Fraktion=".intval($_SESSION[charpics][fraktion])." AND Editor=".intval($_POST[editor])." AND Typ=".intval($_POST[typ])." AND Name='$datei[0]' AND Need=".intval($_POST[need]).";");
    if($bild=$db->fetch_assoc())
      $id=$bild[Id];
    else
    {
      $db->query("INSERT INTO charpics(Editor, Fraktion, Typ, Need, Name) VALUES(".intval($_POST[editor]).",".intval($_SESSION[charpics][fraktion]).", ".intval($_POST[typ]).",".intval($_POST[need]).",'".$db->real_escape_string($datei[0])."');");
      //id vom neuen Eintrag ermitteln
	  $db->query("SELECT Max(Id) AS Id FROM charpics;");
      $id=$db->result(0,"Id");
    }
    move_uploaded_file($_FILES[bild]["tmp_name"],"images/kommander/kommander$id.png");
  }  
}

//Import
if($_POST['do']=="import"){
    move_uploaded_file($_FILES[datei][tmp_name], "tmp/import.btsun");
    $import=unserialize(file_get_contents("tmp/import.btsun"));
	$sql=explode("\n", $import[db]);
    foreach($sql as $value){
        if($value!=""){
            $db->query($value);
		}
	}
	//db laden
	foreach($import[bild] AS $key=>$value){
	    file_put_contents("images/kommander/kommander".$key.".png", $value);	
	}
}
echo "
<fieldset>
  <legend>Options</legend>
    <table width=\"100%\">
	  <tr>
	    <td align=\"right\">
		  <b>Fraktion:</b>
		</td>
		<td align=\"left\">
		  <select onchange=\"location.href='index.php?fraktion='+this.value;\">";
          foreach($config_fraktion as $key=>$value){
	          if(isset($value[name])){
                  if($key==$_SESSION[charpics][fraktion]){
                      echo "<option value=\"$key\" selected=\"selected\">$value[name]</option>";
			      }else{
			          echo "<option value=\"$key\">$value[name]</option>";
			      }
		      }
	      }
          echo "
          </select>
        </td>		  
		<td align=\"right\"> 
		  <b>Import:</b>
		</td>
	    <td align=\"left\">
          <form action=\"$_SERVER[PHP_SELF]\" method=\"post\" enctype=\"multipart/form-data\">
            <input type=\"hidden\" name=\"do\" value=\"import\" />
            <input name=\"datei\" type=\"file\" size=\"10\" maxlength=\"100000\" onchange=\"this.form.submit();\"/>
          </form>
		</td>
		<td align=\"right\"> 
		  <b>Export:</b>
		</td>
		<td align=\"left\">
          <input type=\"button\" value=\"export\" onclick=\"location.href='index.php?page=admin&adminpage=charpics&do=export';\" />";
          //Exportieren
          if($_GET['do']=="export"){
              $export[db]=$db->backup("charpics");
              $abfrage="SELECT * FROM charpics;";
              $result=$db->query($abfrage);
              while($pic=$db->fetch_assoc($result)){
                  $export[bild][$pic[Id]]=file_get_contents("images/kommander/kommander".$pic[Id].".png");
              }
              file_put_contents("tmp/kommander_export.btsun",serialize($export));
              echo "<a href=\"tmp/kommander_export.btsun\">download</a>";  
          }
          echo "
		</td>
	  </tr>
	</table>
</fieldset>"; 
//beginn des eigentlichen tools
  
echo "
<fieldset>
  <legend>Chareditor (erste Ebende~Fixed)</legend>
  <table width=\"100%\">
    <tr>
      <th rowspan=\"5\">
        <img src=\"bild.php\">
      </th>
      <th>Grundmodell:</th>
      <th>
	    ".imageselect(1,0)."
	  	<input type=\"button\" value=\"X\" onclick=\"if(confirm('erase?')) location.href='index.php?page=admin&adminpage=charpics&erase='+(document.getElementById('selectbox0').value);\">
      </th>
      <th>".uploadform(1,0)."</th>
    </tr>
    <tr>
      <th>Kopf:</th>
      <th>
	    ".imageselect(1,1,$_SESSION[charpics]["1-0"])."
	    <input type=\"button\" value=\"X\" onclick=\"if(confirm('erase?')) location.href='index.php?page=admin&adminpage=charpics&erase='+(document.getElementById('selectbox1').value);\">
	  </th>
      <th>".uploadform(1,1,$_SESSION[charpics]["1-0"])."</th>
    </tr>
    <tr>
      <th>Gesicht:</th>
      <th>
	    ".imageselect(1,2,$_SESSION[charpics]["1-0"])."
        <input type=\"button\" value=\"X\" onclick=\"if(confirm('erase?')) location.href='index.php?page=admin&adminpage=charpics&erase='+(document.getElementById('selectbox2').value);\">
	  </th>
      <th>".uploadform(1,2,$_SESSION[charpics]["1-0"])."</th>
    </tr>
    <tr>
      <th>Körper:</th>
      <th>
	    ".imageselect(1,3,$_SESSION[charpics]["1-0"])."
        <input type=\"button\" value=\"X\" onclick=\"if(confirm('erase?')) location.href='index.php?page=admin&adminpage=charpics&erase='+(document.getElementById('selectbox3').value);\">
	  </th>
      <th>
	    ".uploadform(1,3,$_SESSION[charpics]["1-0"])."
	  </th>
    </tr>        
    <tr>
      <th>für Mitch:</th>
      <th>
	    ".imageselect(1,4,$_SESSION[charpics]["1-0"])."
        <input type=\"button\" value=\"X\" onclick=\"if(confirm('erase?')) location.href='index.php?page=admin&adminpage=charpics&erase='+(document.getElementById('selectbox4').value);\">
	  </th>
      <th>
	    ".uploadform(1,4,$_SESSION[charpics]["1-0"])."
	  </th>
    </tr>        
  </table>
</fieldset>";

          
      
  
  